package com.weetop.akly.activity.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;

import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.weetop.akly.R;
import com.weetop.akly.activity.meessage.ShenpiDetailActivity;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.bean.VoteItem;
import com.weetop.akly.bean.VoteItem;
import com.weetop.akly.presenter.VoteCenterPresent;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.view.VoteCenterView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VoteCenterActivity extends BaseActivity<VoteCenterPresent> implements VoteCenterView {

    @BindView(R.id.tv_tl_title)
    TextView tvTlTitle;
    @BindView(R.id.tv_tl_right)
    TextView tvTlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rcy_record)
    RecyclerView rcyRecord;
    @BindView(R.id.srl_record)
    SmartRefreshLayout srlSpRecord;
    private ArrayList<VoteItem> data = new ArrayList<>();
    private boolean isRefresh, isLoadMore;
    private int page = 1;

    @Override
    protected VoteCenterPresent createPresenter() {
        return new VoteCenterPresent(this, this);
    }

    private CommonRecyclerAdapter<VoteItem> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_votecenter);
        ButterKnife.bind(this);
        setToolBar("投票中心", "");
        intListView();
        mPresenter.getVoteList(page + "");
    }

    private void intListView() {
        rcyRecord.setItemAnimator(null);
        rcyRecord.setLayoutManager(new LinearLayoutManager(mActivity));
        srlSpRecord.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                isLoadMore = true;
                page++;
                mPresenter.getVoteList(page + "");
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                isRefresh = true;
                page = 1;
                mPresenter.getVoteList(page + "");
            }
        });
    }

    @Override
    public void getVoteListSuccess(BaseModel<ArrayList<VoteItem>> o) {
        ArrayList<VoteItem> items = o.getData();
        showDataInList(items);
    }

    private void showDataInList(ArrayList<VoteItem> items) {
        if (adapter == null) {
            data.clear();
            data.addAll(items);
            adapter = new CommonRecyclerAdapter<VoteItem>(mActivity, R.layout.item_vote, data) {
                @Override
                public void onUpdate(BaseAdapterHelper helper, VoteItem item, int position) {
                    helper.setText(R.id.tv_item_title, item.getTitle());
                    // 选项：-1拒绝；0未投票；1同意
                    String status;
                    int statusBg;
                    String statusTextColor;
                    switch (item.getStatus()) {
                        case 0:
                            statusBg = R.drawable.bg_state_errortag;
                            statusTextColor = "#FF4C23";
                            status = "未投票";
                            break;
                        default:
                            statusBg = R.drawable.bg_state_oktag;
                            statusTextColor = "#4CCF72";
                            status = "已投票";
                            break;
                    }
                    helper.setText(R.id.tv_status, status);
                    helper.setTextColor(R.id.tv_status, Color.parseColor(statusTextColor));
                    helper.setBackgroundRes(R.id.tv_status, statusBg);
                    helper.setText(R.id.tv_right, BaseUtils.covnDate(item.getStart_time()));
                    String type = item.getType() == 2 ? "升职" : "面谈";
                    helper.setText(R.id.tv_one, "备注内容：" + type);
                    helper.setText(R.id.tv_two, "开始时间：" + item.getStart_time());
                    helper.setText(R.id.tv_three, "结束时间：" + item.getEnd_time());
                }
            };
            rcyRecord.setAdapter(adapter);
            adapter.setOnItemClickListener((viewHolder, view, position) -> {
                if (BaseUtils.fastClick()) {
                    Intent intent = new Intent();
                    intent.putExtra("vote_id", data.get(position).getId());
                    BaseUtils.toActivity(mActivity, VoteDetailActivity.class, intent);
                }
            });
        } else {
            if (isRefresh) {
                data.clear();
                data.addAll(items);
                srlSpRecord.finishRefresh(true);
                isRefresh = false;
                adapter.replaceAll(data);
            } else if (isLoadMore) {
                data.addAll(items);
                adapter.addAll(items);
                isLoadMore = false;
            }
        }
        if (items == null || items.size() < 10) {
            srlSpRecord.finishLoadMoreWithNoMoreData();
        } else {
            srlSpRecord.finishLoadMore(true);
        }
    }
}